/**
 * 
 */

function validateForm() {
	var error = document.getElementById("error");
	while (error.hasChildNodes()) {
		error.removeChild(error.childNodes[0]);
	}
	var email = document.getElementById("memberEmail").value;
	var username = document.getElementById("username").value;
	var password = document.getElementById("memberPassword").value;
	var check = true;
	var textError = null;
	if (email == null || email == "") {
		textError = "Email must be filled out!";
		addElement(error, textError);
		check = false;
	} else if (checkExistedEmail()) {
		textError = "Email has existed!";
		addElement(error, textError);
		check = false;
	}
	if (username == null || username == "") {
		textError = "Username must be filled out!";
		addElement(error, textError);
		check = false;
	} else if (checkExistedUsername()) {
		textError = "Username has existed!";
		addElement(error, textError);
		check = false;
	}
	if (password == null || password == "") {
		textError = "Password must be filled out!";
		addElement(error, textError);
		check = false;
	}
	if (birthday != null || birthday != "") {
		var birthday = new Date(document.getElementById("birthday").value);
		var today = new Date();
		if (today < birthday) {
			textError = "Birthday is invalid!";
			addElement(error, textError);
			check = false;
		}
	}
	return check;
}

function validateFormUpdate() {
	var error = document.getElementById("error");
	while (error.hasChildNodes()) {
		error.removeChild(error.childNodes[0]);
	}
	var email = document.getElementById("memberEmail").value;
	var password = document.getElementById("memberPassword").value;
	var emailBefore = document.getElementById("email").value;
	var check = true;
	var textError = null;
	if (email == null || email == "") {
		textError = "Email must be filled out!";
		addElement(error, textError);
		check = false;
	} else if (email != emailBefore) {
		if (checkExistedEmail()) {
			textError = "Email has existed!";
			addElement(error, textError);
			check = false;
		}
	}
	if (password == null || password == "") {
		textError = "Password must be filled out!";
		addElement(error, textError);
		check = false;
	}
	if (birthday != null || birthday != "") {
		var birthday = new Date(document.getElementById("birthday").value);
		var today = new Date();
		if (today < birthday) {
			textError = "Birthday is invalid!";
			addElement(error, textError);
			check = false;
		}
	}
	return check;
}

function addElement(div, textError) {

	var dv = document.createElement("DIV");
	dv.setAttribute("class", "alert alert-danger");
	var sp1 = document.createElement("SPAN");
	sp1.setAttribute("class", "glyphicon glyphicon-exclamation-sign");
	dv.appendChild(sp1);

	var text = document.createTextNode(textError);

	dv.appendChild(text);
	div.appendChild(dv);
}

function checkExistedUsername() {
	var flag = false;
	$.ajax({
		"type" : "POST",
		"async" : false,
		"url" : "/core-web-admin/member/checkexistedusername",
		"data" : {
			"username" : $('#username').val()
		},
		"success" : function(data) {
			if (data == "true") {
				flag = true;
			}
			if (data == "false") {
				flag = false;
			}
		},
		"error" : function(data) {
			alert("ERROR WHEN CHECK EXISTED USER NAME!");
			flag = false;
		}
	});
	return flag;
}

function checkExistedEmail() {
	var flag = false;
	$.ajax({
		"type" : "POST",
		"async" : false,
		"url" : "/core-web-admin/member/checkexistedemail",
		"data" : {
			"email" : $('#memberEmail').val()
		},
		"success" : function(data) {
			if (data == "true") {
				flag = true;
			}
			if (data == "false") {
				flag = false;
			}
		},
		"error" : function(data) {
			alert("ERROR WHEN CHECK EXISTED EMAIL!");
			flag = false;
		}
	});
	return flag;
}

function backPage() {
	history.back();
}